// pages/domain/domain.js 域见
var shihan = require('../../common/fetch')
var config = require('../../common/config')
var app = getApp()
var winScale = 750 / app.data.deviceInfo.windowWidth;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isoffline: true,
    currentTab: 0,
    currentdomainItem: 0,
    domaininfo: [],
    showLoading: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    that.checknetword(options)
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          winHeight: res.windowHeight
        });
      }
    });
  },
  getDomain: function() {
    var that = this;
    shihan.sendRequest(config.domain, 'GET', {
      preview: 1
    }, {})
      .then(function (response) {
        if (response.data.code == 0) {
          that.setData({
            showLoading: false,
            domaininfo: response.data.data
          });
          wx.setStorageSync('domain_pic_cache', response.data.data)
        }
      }, function (error) {
        console.log(error);
      });
  },
  refreshdata: function () {
    this.setData({
      showLoading: true
    })
    this.onShow();
  },
  checknetword: function options() {
    var that = this
    wx.getNetworkType({
      success: function(res) { // wifi/2g/3g/4g/unknown(Android下不常见的网络类型)/none(无网络)
        // console.log(res)
        if (res.networkType == 'none') {
          that.setData({
            isoffline: false,
            showLoading: false,
            offoptions: options
          })
          wx.stopPullDownRefresh()
        } else {
          that.setData({
            isoffline: true,
            showLoading: true
          })
        }
      }
    })
  },
  domainChange: function(e) {
    var that = this;
    that.setData({
      currentdomainItem: e.detail.current
    });
  },
  domainDetail: function(e) {
    var that = this;
    wx.navigateTo({
      url: '/pages/other/domaindetail/domaindetail?province_id=' + e.currentTarget.dataset.id + '&topimg=' + e.currentTarget.dataset.img
    })
  },
  onNavbarTap: function(e) {
    var index = e.currentTarget.dataset.index
    this.setData({
      currentTab: index,
      navbarleft: ((index + 1) * 100 - 50 - 375) / winScale
    });
    // if( )
  },
  domainChange: function(e) {
    this.setData({
      currentTab: e.detail.current,
      navbarleft: ((e.detail.current + 1) * 100 - 50 - 375) / winScale
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.checknetword(this.data.offoptions);
    var domain_cache = wx.getStorageSync('domain_pic_cache');
    if (domain_cache) {
      this.setData({
        showLoading: false,
        domaininfo: domain_cache
      });
    }
    this.getDomain();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  }
})